package calculator;

import java.math.BigDecimal;
import java.util.HashMap;

public class VariableMap {
	
	private static VariableMap instance=null;
	private HashMap<String,BigDecimal> valuemap;
	private HashMap<String, Integer> typemap; 
	public static final int CONSTANT=1;
	public static final int VARIABLE=0;
	
	
	/** Builds a new VariableMap Object */
	private VariableMap()
	{
		valuemap = new HashMap<String,BigDecimal>();
		typemap = new HashMap<String,Integer>();
	}
	
	/** Returns an instance of the variableMap Class */
	public static VariableMap getInstance()
	{
		
			if(instance == null)
			{
				instance = new VariableMap();
			}
		
			return instance;
		
	}
	
	/** Adds a variable.  Returns true if the operation was successful, false if the user tried to modify a constant */
	public boolean addVariable(String var, BigDecimal val, int type )
	{
		if(typemap.containsKey(var)&&typemap.get(var)==VariableMap.CONSTANT)
		{
			return false;
		}
		else
		{
			valuemap.put(var, val);
			typemap.put(var, type);
			return true;
		}
		
	}
	
	/** Returns the value of the specified variable */
	public BigDecimal getVarValue(String var)
	{	
		return valuemap.get(var);
	}
	
	/** Returns true if the variable is defined, false otherwise */
	public boolean varExists(String var)
	{
		return valuemap.containsKey(var);
	}
	
	
	

}
